//
//  FirstViewController.m
//  练习11
//
//  Created by 张琦 on 15/11/20.
//  Copyright © 2015年 张琦. All rights reserved.
//

#import "FirstViewController.h"
#import "AFNetworkToll.h"
#import "ModelFind.h"
#import "CellFirstFind.h"
#import "CellFirstFindRect.h"
#import "CellFirstFindHotSongs.h"
#import "CellFirstFindNewSongs.h"
#import "CellFirstFindLastedNewSongs.h"
#import "CellFirstFindLastedNewMV.h"
#import "SecondViewController.h"
#define SCR_WIDTH     [UIScreen mainScreen].bounds.size.width
#define SCR_HEIGHT    [UIScreen mainScreen].bounds.size.height


@interface FirstViewController ()<UITableViewDataSource, UITableViewDelegate, CellFirstFindRectDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic, retain) NSMutableArray *array1;
@end

@implementation FirstViewController

- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCR_WIDTH, SCR_HEIGHT - 64 - 60) style:UITableViewStylePlain];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.tableView registerClass:[CellFirstFind class] forCellReuseIdentifier:@"CellFirstFind"];
    [self.tableView registerClass:[CellFirstFindRect class] forCellReuseIdentifier:@"CellFirstFindRect"];
    [self.tableView registerClass:[CellFirstFindHotSongs class] forCellReuseIdentifier:@"CellFirstFindHotSongs"];
    [self.tableView registerClass:[CellFirstFindNewSongs class] forCellReuseIdentifier:@"CellFirstFindNewSongs"];
    [self.tableView registerClass:[CellFirstFindLastedNewSongs class] forCellReuseIdentifier:@"CellFirstFindLastedNewSongs"];
    [self.tableView registerClass:[CellFirstFindLastedNewMV class] forCellReuseIdentifier:@"CellFirstFindLastedNewMV"];

//    self.tableView.scrollEnabled = NO;
}

// 行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 90 * 3 + 40 + 20;
    } else if (indexPath.row == 1) {
        return 140 * 2 + 40 + 20;
    } else if (indexPath.row == 2) {
        return 140 + 40 + 20;
    } else if (indexPath.row == 3) {
        return 90 + 40 + 20;
    } else if (indexPath.row == 4) {
        return 130 + 40 + 20;
    } else if (indexPath.row == 5) {
        return 130 + 40 + 20;
    }
    return 100;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.array.count == 0) {
        return 0;
    }
    return self.array.count - 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ModelFind *model = [self.array objectAtIndex:indexPath.row + 2];
    if (indexPath.row == 0) {
        CellFirstFind *cell = [tableView dequeueReusableCellWithIdentifier:@"CellFirstFind"];
        NSDictionary *dic = [model.data firstObject];
        NSArray *array = [dic objectForKey:@"songs"];
        cell.name = [dic objectForKey:@"name"];
        cell.array = array;
        cell.target = self;
        cell.action = @selector(handleMenListening);
        return cell;
    } else if (indexPath.row == 1) {
        CellFirstFindRect *cell = [tableView dequeueReusableCellWithIdentifier:@"CellFirstFindRect"];
        cell.delegate = self;
        cell.name = model.name;
        cell.array = model.data;
        return cell;
    } else if (indexPath.row == 2) {
        CellFirstFindHotSongs *cell = [tableView dequeueReusableCellWithIdentifier:@"CellFirstFindHotSongs"];
        cell.name = model.name;
        cell.array = model.data;
        return cell;
    } else if (indexPath.row == 3) {
        CellFirstFindNewSongs *cell = [tableView dequeueReusableCellWithIdentifier:@"CellFirstFindNewSongs"];
        ModelFind *model = [self.array objectAtIndex:indexPath.row + 3];
        cell.name = model.name;
        cell.array = model.data;
        return cell;
    } else if (indexPath.row == 4) {
        CellFirstFindLastedNewSongs *cell = [tableView dequeueReusableCellWithIdentifier:@"CellFirstFindLastedNewSongs"];
        ModelFind *model = [self.array objectAtIndex:indexPath.row + 3];
        cell.name = model.name;
        cell.array = model.data;
        return cell;
    } else if (indexPath.row == 5) {
        CellFirstFindLastedNewMV *cell = [tableView dequeueReusableCellWithIdentifier:@"CellFirstFindLastedNewMV"];
        ModelFind *model = [self.array objectAtIndex:indexPath.row + 3];
        cell.name = model.name;
        cell.array = model.data;
        return cell;

    }
    CellFirstFind *cell = [tableView dequeueReusableCellWithIdentifier:@"CellFirstFind"];
    return cell;
}

- (void)pushPage:(NSString *)value
{
    SecondViewController *second = [[SecondViewController alloc] init];
    second.url = [NSString stringWithFormat:@"http://api.songlist.ttpod.com/songlists/%@?address=%E8%BE%BD%E5%AE%81%E7%9C%81%E5%A4%A7%E8%BF%9E%E5%B8%82%E6%B2%99%E6%B2%B3%E5%8F%A3%E5%8C%BA%E4%BA%94%E4%B8%80%E8%B7%AF314%E5%8F%B7&agent=none&longitude=121.54475&utdid=Vb2Wazwk%2BQ8DAKMS4xW3NnBK&net=2&from=android&os=5.0.1&v=v8.3.0.2015110214&alf=702006&api_version=1.0&imei=867246027510362&latitude=38.883915&f=3030&resolution=1080x1920&language=zh&user_id=0", value];
    [self.navigationController pushViewController:second animated:YES];
    [second release];
}

- (void)handleMenListening
{
    SecondViewController *second = [[SecondViewController alloc] init];
    second.url = @"http://api.favorite.ttpod.com/favorite/song/plaza?address=%E8%BE%BD%E5%AE%81%E7%9C%81%E5%A4%A7%E8%BF%9E%E5%B8%82%E6%B2%99%E6%B2%B3%E5%8F%A3%E5%8C%BA%E4%BA%94%E4%B8%80%E8%B7%AF314%E5%8F%B7&agent=none&longitude=121.54474&utdid=Vb2Wazwk%2BQ8DAKMS4xW3NnBK&net=2&from=android&os=5.0.1&v=v8.3.0.2015110214&alf=702006&api_version=1.0&imei=867246027510362&latitude=38.883896&f=3030&resolution=1080x1920&language=zh&random=0&user_id=0";
    [self.navigationController pushViewController:second animated:YES];
    [second release];

}

- (void)loadData
{
    
    self.array = [NSMutableArray array];
    // 视频网址
    NSString *urlStr = @"http://api.dongting.com/frontpage/frontpage?v=v8.3.0.2015110214&f=f3030&s=s200&version=1448017206";
    [AFNetworkToll getUrl:urlStr body:nil response:ZHResponseStyleJSON requestheadFile:nil success:^(NSURLSessionDataTask *task, id result) {
        
        NSArray *array = [result objectForKey:@"data"];
        for (NSDictionary *dic in array) {
            ModelFind *model = [[ModelFind alloc] initWithDic:dic];
            [self.array addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error === %@", error);
    }];
}


- (void)loadData11
{
    
    // 视频网址
    NSString *urlStr = @"http://so.ard.iyyin.com/s/lrcid?sign=3975138baec8f87e3834a389f9cbac14&lrcid=15707&utdid=VTIhBu5lN90DAPOOoXsWE1xU&title=明天你好&artist=牛奶咖啡&type=3";
    [AFNetworkToll getUrl:urlStr body:nil response:ZHResponseStyleJSON requestheadFile:nil success:^(NSURLSessionDataTask *task, id result) {
        
        NSString *str = [[result objectForKey:@"data"] objectForKey:@"content"];
        NSString *s1 = [self filterHTML:str];
        NSLog(@"===%@", [self filterHTML1:s1]);
        
        
        dispatch_async(dispatch_get_main_queue(), ^{
        });
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error === %@", error);
    }];
}

- (NSString *)filterHTML:(NSString *)html
{
    NSScanner * scanner = [NSScanner scannerWithString:html];
    NSString * text     = nil;
    while([scanner isAtEnd]==NO)
    {
        //找到标签的起始位置
        [scanner scanUpToString:@"<" intoString:nil];
        //找到标签的结束位置
        [scanner scanUpToString:@">" intoString:&text];
        
        //替换字符
        html = [html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@>",text] withString:@""];
        

    }
    //    NSString * regEx = @"<([^>]*)>";
    //    html = [html stringByReplacingOccurrencesOfString:regEx withString:@""];
    return html;
}
- (NSString *)filterHTML1:(NSString *)html
{
    NSScanner * scanner = [NSScanner scannerWithString:html];
    NSString * text     = nil;
    while([scanner isAtEnd]==NO)
    {
        //找到标签的起始位置
        [scanner scanUpToString:@"[" intoString:nil];
        //找到标签的结束位置
        [scanner scanUpToString:@"]" intoString:&text];
        
        //替换字符
        html = [html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@]",text] withString:@""];
        
        
    }
    //    NSString * regEx = @"<([^>]*)>";
    //    html = [html stringByReplacingOccurrencesOfString:regEx withString:@""];
    return html;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"发现";
    [self createTableView];
    [self loadData];
    [self loadData11];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
